﻿using System;
using System.Collections.Generic;
using System.Text;
using Kingdee.BOS.Core.CloudModel.Crm;
using Newtonsoft.Json;
using Kingdee.BOS.Core.CloudModel;

namespace MethodSetBus
{
    public class Crm_QueryBus
    {
        public string Get_CRM_Query(string Type,string key,string token,string Count,string num,List<CrmWhere> wheres)
        {
            Crm_Query _Query = new Crm_Query();
			_Query.module = Type;
			_Query.func = "getList";
			_Query.apikey = key;
			_Query.token = token;
			_Query.username = "admin";
			_Query.pagesize = Count;
			_Query.pagenum = num;

            if (wheres != null && wheres.Count > 0)
            {
				List<searchtextEntry> entries = new List<searchtextEntry>();
				foreach (CrmWhere item in wheres)
                {
					searchtextEntry entry = new searchtextEntry();
					entry.groupid = 1;
					entry.fieldname = item.WhereSize;
					entry.module = Type;
					entry.comparator = item.WhereType;
					entry.value = item.WhereText;
					entry.columncondition = item.WhereEmpa;
					entry.groupcondition = item.WhereEmpa;
					entries.Add(entry);
				}
				_Query.searchtext = entries;
			}
			
			return JsonConvert.SerializeObject(_Query);

		}



		public string Get_Crm_MySqlQueryStateMent(string Type,NeedsNumbers data)
        {
			string Query = "";
            try
            {
                switch (Type)
                {
					case "SaleOrder":
						Query = $@"
                                select a.quote_no 报价单号,a.approvestatus 审批状态,a.baojia_type 报价单类型,b.cf_5364 套餐名,c.accountname 客户名称,staff.last_name 销售负责人,b.cf_5347 订单折扣率,cast(a.total as decimal(18,4)) 合计 from ld_quotes a 
                                left join ld_quotescf b on a.quoteid = b.quoteid
                                left join ld_account c on a.accountid = c.accountid/*客户*/
                                left join ld_users staff on a.smownerid = staff.id/*员工*/
                                where FIND_IN_SET(a.quoteid, (select source_list from ld_salesorder where salesorder_no= '{data.Number}'));";
						break;
					case "Quotation":
						Query = $@"
                                 select a.quote_no 报价单号,w.product_no 物料编码,w.productname 物料名称,b.quantity 数量,w.productid 物料ID from ld_quotes a 
                                 left join ld_quotesdetail b on a.quoteid=b.id
                                 LEFT JOIN ld_products w on b.productid=w.productid
                                 where a.quote_no='{data.Number}';
                                 ";
						break;
                }
				return Query;
            }
            catch (Exception ex)
            {
				return Query;
            }
        }




	}
}
